package com.xiaoyao.novel.parser;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.xiaoyao.novel.crawl.BaseCrawl;
import com.xiaoyao.novel.pojo.Book;
import com.xiaoyao.novel.pojo.Chapter;
import com.xiaoyao.novel.util.ClassUtil;
import com.xiaoyao.novel.util.FileUtil;

public class Buy773Parser {
	private static Logger logger = Logger.getLogger(Buy773Parser.class);

	public static List<Chapter> parseBook(String page, Book book) {
		logger.debug("开始解析...");
		page = page.replaceAll("\"", "'");
		page = page.replaceAll("\\s+", " ");
		if (page.contains("<span class='l'>正文</span>")) {
			page = page.replaceFirst(".+<span class='l'>正文</span>", " "); 
		}
		List<Chapter> list = new ArrayList<Chapter>();
		book.list = list;
		//<td><div class="dccss"><a href="15954.html" alt="楔子">楔子</a></div></td>(.+?)
		String bookRegex = "<td><div class='dccss'><a href='(.+?)'.*?>(.+?)</a></div></td>";
		Pattern bookPattern = Pattern.compile(bookRegex);
		Matcher matcher = bookPattern.matcher(page);
		while (matcher.find()) {
			String title = matcher.group(2).trim();
//			if (title.startsWith("引子") || title.startsWith("前言") ||title.contains("第") || title.contains("章")  || title.matches("\\d+.+")) {
				logger.info(title);
				Chapter chapter = new Chapter();
				list.add(chapter);
				chapter.bookId = book.bookId;
				chapter.chapterUrl = book.bookUrl+  matcher.group(1);
//				chapter.webId =  matcher.group(2);
				chapter.chapterTitle = title;
				logger.debug(chapter);
//			}
		}
		return list;
	}

	public static void parseChapter(String page, Chapter chapter) {
		page = page.replaceAll("\"", "'");
		page = page.replaceAll("\\s+", " ");
		logger.debug("页面:" + page);
		String chapterRegex = "<DIV id=content.+?>(.+?)</DIV>";
		Pattern chapterPattern = Pattern.compile(chapterRegex);
		Matcher matcher = chapterPattern.matcher(page);
		if (matcher.find()) {
			chapter.chapterContent = matcher.group(1).replaceAll("<script.+?</script>", "");
			logger.debug(chapter.chapterContent);
		}
	}

	/**
	 * 作者,[书名],简介,[类型],图片
	 * 
	 * @param page
	 * @param book
	 * @return
	 */
	public static Book parseDescrib(String page, Book book) {
		page = page.replaceAll("\"", "'");
		page = page.replaceAll("\\s+", " ");
		page = page.replaceAll("&nbsp;", " ");
		logger.debug("页面:" + page);
		String infoRegex = "<h1>\\s*您当前的位置.+?</a>.+?<a href='(/html/.+?/)'.*?>(.+?)</a></h1>" 
			+ ".+?<img.+?src='(.+?)'.+?书籍作者.+?<td.+?>(.+?)</td>.*?<div id='CrbsSum'>【作品简介】：(.+?)</div>";
		Pattern infoPattern = Pattern.compile(infoRegex);
		Matcher matcher = infoPattern.matcher(page);
		if (matcher.find()) {
			// book.bookName = matcher.group(1);
			if (ClassUtil.isEmpty(book.picPath)) {
				String picUrl = matcher.group(3);
				String fileName = book.bookId + ".jpg";
				fileName = BaseCrawl.downLoadImage(picUrl, fileName); // 抓图片
				book.picPath = FileUtil.PIC_DIR + fileName;
				book.bookUrl=matcher.group(1);
			}
			book.authorName = matcher.group(4).replaceAll("<.+?>", "");
			if (ClassUtil.isEmpty(book.describ)) {
				book.describ = matcher.group(5);
			}
//			logger.info("ireader:"+book.bookName+","+book.authorName);
		} else {
			logger.error("简介有问题!" + book.crawler);
			logger.error("页面" + page);
		}

		return book;
	}
}
